import base64
import hashlib
import urllib.parse

from Crypto import Random
from Crypto.Hash import SHA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.PublicKey import RSA
import json

if __name__ == '__main__':
    request = {
        "accessToken": "QXFxUnI3eHYveWNnNDVWMkhyV2F2RzRac2ZSUTg5T3EwM1BYUnFHalBvU0JETjBNN3JGM3B6N3lWSkc4K0pWQm1tZklXZVZVTkMvOHFsdngrbkpSckhWM2R0TGwvU3R3SjV0bk4wV3VWN1E9",
        "android_id": "89b55ae86c63da6b",
        "imei": "866174491984528",
        "introduction": "V7HzWaKgyKmjwTkDZk5%2F9w%3D%3D",
        "isVM": "0",
        "loginType": "4",
        "mac": "08:00:27:2D:D8:96",
        "pid": "104725",
        "platform": "Android",
        "refreshToken": "Sm1SbGJQZzF5WDUyMkhVdUV2ZXFrcDNkK3BTTHRTS2FuOVplZS9WT0xjeTQ4aHdEK2FYRWtKMmVMN0NjT1RXNDlVRHMzVnlaRFZuY0pWdnJkT1hGUTBUVGJkMW9xbVF6SkRlRHlyYktSVjQ9",
        "signType": "RSA",
        "tel": "13815335078",
        "timestamp": "2021-04-14 10:41:46",
        "unit": "OPPO R11 Plus",
        "username": "UC191812",
        "version": "1.1",
        "sign": "Mc3Dtnr/t4Ur4PdIOPzVKEPN1ciOIhn4ujfu7ePzQgXt6yMOnxM+rZcSH8Ss78E041m0o+T9CEGC\nwS6mOKAfihW5GOYDatY9hwWVrtc7MmVAOsTiJKHRESloEiEiTboMZkuuG4DRM3LCPOqDiKQp2r9v\nAkV555IuQCHIrUrEwTTHwx/1cNkzsHjNLqZoJeHAgHNVDuAe+TEdaVFpUDKYqZZKy6iJXaXLFTu/\nf4rncgHjg+mowbp5Aww0+ZrHMeoUuxHBXwfvrqNr9ikwH1qlgW/E0m/MCP8cvBVAbV6fKrgynIyT\nssSWNBfNL+58BxHR449ElxYB1rG09TYfDoUrBg\u003d\u003d\n"
    }

    gameKey = 'oHz0RO6lhbLwbXyMb4MZwg%3D%3D'  # 104725
    keys = sorted(request.keys())
    print('keys:' + str(keys))

    signParams = ''
    for k in keys:
        if request[k] != '' and request[k] != None and k != 'sign' and '@' != str(request[k])[0]:
            # signParams += k + '=' + urllib.parse.quote(request[k]) + '&'
            signParams += k + '=' + request[k] + '&'
    signParams += 'key=' + gameKey

    print('signParams:' + signParams)

    m = hashlib.md5(signParams.encode())

    sign_md5 = m.hexdigest()

    print('sign md5:' + sign_md5)
    print('----')

    ## "sign": "0XGp1K4fORp8ezteL/eSqKmNDa1kXyAKRcaIEKQlLZaAxIfmGoMs1K26odIUmJVHC4MJrgj0zdp0\ntAkIL2W5aDFx5+8iM5sh332AIr6/pUjRC5QVRVLYQ27BLU/rVUeeEYU5PNkwc1064bOLZGkX0onH\nG9pV2bi+tCfdh5PipNZcKBxrYtJaioca4PE/NW0mAarusaSe0meUl/VnCZbuDy59sdZ0Lc3YKmH0\nNG4rNqz1M5+m97Uf7wzhyaEs/SfmOXDUNil0zeQ+mSxOkBaLM/eJBM3VdwUM6KxUUUGcuUAxkL8G\nALggHhWN3O8HUM+xzq/2pg4uXIns6cgvK/ipRA\u003d\u003d\n"
    Private = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCyOoR49HbG8IXNWCE+W7D/SPpTKeedaZlB5401rNdpSeMANXEkXBfEwf1A7UyIoUrWX5JJtOuxQD0Wu3fcAiRMaxLiLiOUnwV15olAqFVbI/XFzmLG2lpEWgybEaq/FGULndzGdBFtTixT39va3tMZ3ncwtY3eZbQ7EVrulzvhJlUZ79yZlP9eOMlqX++7N/MTvmotXzR46G2NoI1kPOy6wXkJ/HI9plPHuPRIw/DN8zRHfCxo+yYQ07n6+Sfjud3KHqKvy3jkFfl5wOf5qHMXYiemvVYCLC9MIZ1WlXVS6BP1fXI74MOTIJgw0R6tVi7VqnxXBchjZWt0uu2kZw8bAgMBAAECggEAUSL4qc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/+KSt4geWeAS21ciKFIVi38S8deoGLX1/ydHkDhiHD+pjIVXNs73JGwm3qZ7bJmn6w4J8dnyYvnTOyxbj9Gs7MvqyXemtT44Q684ctmraizVlGwkvEW+LS1Rr/BlIH4GjrYiuc'
    with open("master-public.pem") as f:
        key = f.read()
    rsakey = RSA.importKey(key)
    signer = Signature_pkcs1_v1_5.new(rsakey)
    digest = SHA.new()
    digest.update(sign_md5.encode())
    sign = signer.sign(digest)
    signature = base64.b64encode(sign)
    print(signature.decode())
    Lastsign = signature.decode()
    fileSign = open(r'sign.txt', mode='w')
    fileSign.write(Lastsign)




